from backend.controller.util import *
from backend.models import Performance, Argument, User, Project, Achievement, Service, \
    Research


def save(request):
    user_id, topic, argument_info = (
        format_param(request, 'user_id', 'topic', 'argument'))

    if not user_id:
        return format_response(WRONG_PARAM)

    try:
        user = User.objects.get(id=user_id)
    except:
        return format_response(WRONG_PARAM)

    # 数据校验
    for item in argument_info:
        tmp = {}
        if topic == 'guide':
            tmp = json.dumps(item.get('guide'))
        if topic == 'standard':
            tmp = json.dumps(item.get('standard'))
        if topic == 'application':
            tmp = json.dumps(item.get('application'))
        tmp = json.loads(tmp)
        for item1 in tmp:
            for item2 in item1.values():
                try:
                    float(item2)
                except:
                    return format_response(WRONG_PARAM)

    for item in argument_info:
        print(item.keys())
        try:
            argument = Argument.objects.get(user_id=user_id, project_id=item.get('project_id'))
            if topic == 'guide':
                argument.guide = json.dumps(item.get('guide'))
                argument.save()
            if topic == 'standard':
                argument.standard = json.dumps(item.get('standard'))
                argument.save()
            if topic == 'application':
                argument.application = json.dumps(item.get('application'))
                argument.save()
        except:
            argument = Argument(
                user=user,
                project_id=item.get('project_id'),
                guide=json.dumps(item.get('guide')),
                standard=json.dumps(item.get('standard')),
                application=json.dumps(item.get('application')),
            )
            argument.save()
    return format_response(SUCCESS)


def get_by_user(request):
    """
    根据成员获取绩效信息
    :param request:
    :return:
    """
    user_id = request.GET.get('user_id')
    if not user_id:
        return format_response(WRONG_PARAM)
    item = Argument.objects.get(user_id=user_id)
    data = {
        # 'project_id': item.project_id,
        # 'project': item.project.title,
        'guide': item.guide,
        'standard': item.standard,
        'application': item.application,
    }
    return format_response(SUCCESS, data)


def get_by_project_detail(request):
    user_id = request.GET.get('user_id')
    projects = []
    performance = Performance.objects.all().filter(user_id=user_id)
    for item in performance:
        projects.append(item.project)
    achievement = Achievement.objects.all().filter(user_id=user_id)
    for item in achievement:
        projects.append(item.project)
    projects = list(set(projects))
    head_projects = Project.objects.all().filter(head_user_id=request.session.get('user_id'), is_active=True)
    projects = list(set(projects) & set(head_projects))

    data = []
    for item in projects:
        try:
            argument = Argument.objects.get(project_id=item.id, user_id=user_id)
            data.append({
                'project_id': item.id,
                'project': item.title,
                'guide': argument.guide,
                'standard': argument.standard,
                'application': argument.application
            })
        except:
            data.append({
                'project_id': item.id,
                'project': item.title,
                'guide': [],
                'standard': [],
                'application': []
            })
    return format_response(SUCCESS, data)


def get_by_project(request):
    project_id = request.GET.get('project_id')
    if not project_id:
        return format_response(WRONG_PARAM)
    try:
        item = Argument.objects.get(project_id=project_id)
        data = {
            'guide': item.get_guide_result(),
            'standard': item.get_standard_result(),
            'application': item.get_application_result(),
        }
        return format_response(SUCCESS, data)
    except:
        return format_response(SUCCESS, {
            'guide': 0,
            'standard': 0,
            'application': 0,
        })


def get_list(request):
    user_id = request.session.get('user_id')
    projects = Project.objects.all().filter(head_user=user_id)
    tmp = []
    for item in projects:
        p = Performance.objects.all().filter(project_id=item.id)
        for item1 in p:
            tmp.append(item1.user)
        a = Achievement.objects.all().filter(project_id=item.id)
        for item2 in a:
            tmp.append(item2.user)
    users = list(set(tmp))
    data = []
    for item in users:
        try:
            service = Service.objects.get(user_id=item.id)
            service_mum = service.get_total_service()
        except:
            service_mum = 0

        try:
            research = Research.objects.get(user_id=item.id)
            research_mum = research.get_total_research()
        except:
            research_mum = 0

        data.append({
            "id": item.id,
            "username": item.username,
            "nickname": item.get_full_name(),
            "email": item.email,
            "service": service_mum,
            "research": research_mum
        })
    return format_response(SUCCESS, data)
